<template>
    <div>
        <van-cell-group align="left" style="margin-top: 1rem;margin-bottom: 2rem">
            <van-field v-model="myUserId" label="我的ID" placeholder="请输入用户ID"/>
        </van-cell-group>
        <div style="background-color: white">
            <div id="content" style="height: 20rem; overflow-y: auto" align="center"><p id="content2"></p></div>
        </div>

        <van-cell-group align="left" style="margin-top: 1rem;margin-bottom: 2rem">
            <van-field v-model="userId" label="用户ID" placeholder="请输入用户ID"/>
            <van-field
                    v-model="message"
                    rows="4"
                    autosize
                    label="输入"
                    type="textarea"
                    maxlength="500"
                    placeholder="请输入留言"
                    show-word-limit
            />
        </van-cell-group>


        <button @click="sendMessage(message)">发送</button>
    </div>
</template>

<script>
    import * as signalR from '@microsoft/signalr'

    export default {
        name: "webTest",
        data() {
            return {
                ws: null,
                userId: '',
                //message: '',
                timeout: 570000,
                timeoutObj: null,
                serverTimeoutObj: null,
                myUserId: '10010',
                type: 'lis',
                connection: null,
                message: {
                    Content: {
                        Sfzh: "测试",
                        NickName: "",
                        PhoneNum: ""
                    },
                    From: "",
                    To: "",
                    LocalServerCode: "",
                    TransferCode: ""
                }
            }
        },
        created() {
            this.start();
        },
        destroyed() {

        },
        methods: {

            lingSig() {
                this.signalr.start().then(() => {
                    console.log('连接');
                })
            },

            start() {
                this.connection = new signalR.HubConnectionBuilder()
                    .withUrl("/chatHub", {accessTokenFactory: () => "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoieXV3YW5sb25nIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZWlkZW50aWZpZXIiOiIzNzEzMTIxOTk0MDMwMTUxMTciLCJuYmYiOjE2MDM3NjQ4NjQsImV4cCI6MTYwMzc2NTg2NCwiaXNzIjoiSldUQmVhcmVyLkF1dGgiLCJhdWQiOiJhcGkuYXV0aCJ9.HV3V5uR0FBns-67ggQQbHC0gyZiCUeC6oRLqMaiadK8"})
                    .build();
                //this.connection.on=this.onMessage();
                this.connection.on("msg", (user, message) => {
                    console.log(user)
                    console.log(message)
                });
            },
            msg(user, message) {
                console.log(user);
                console.log(message);
            },
            sendMessage(data) {
                console.log(data)
                this.connection.invoke("SendMessage", 'user', data)
            },

        }

    }
</script>

<style scoped>

</style>